import numpy as np
import numpy.linalg as la
import matplotlib.pyplot as plt
# print(plt.style.available) # uncomment to print all styles
import seaborn as sns
sns.set(font_scale=2)
plt.style.use('seaborn-whitegrid')
plt.rcParams['figure.figsize'] = (12,6.0)
%matplotlib inline
def f(x):
return 0.8 - x + x**2
xx = np.linspace(0,1,1000)
plt.plot(xx, f(xx))
n = 25
t = np.random.rand(n)
y = f(t) + 0.1*np.random.randn(n)*f(t)
plt.plot(xx, f(xx))
plt.plot(t, y, 'ro')
A = np.zeros((n, 3))
A[:,0] = 1
A[:,1] = t
A[:,2] = t**2
ATA = np.dot(A.T, A)
ATb = np.dot(A.T, y)
x = la.solve(ATA, ATb)
print(x)
x0,x1,x2 = x
plt.plot(xx, f(xx))
plt.plot(t, y, 'ro')
plt.plot(xx, x0 + x1*xx + x2*xx**2, 'g-', lw=3)